package com.next

import grails.gorm.transactions.Transactional

@Transactional
class QuestionnaireInstanceService
{
    def initQuestionnaireInstance(Opportunity opportunity)
    {
        def questionnaire = Questionnaire.findByType(QuestionnaireType.findByName(opportunity?.product?.name))
        def questionnaireIns = QuestionnaireInstance.findByQuestionnaireAndOpportunity(questionnaire, opportunity)
        if (questionnaire && !questionnaireIns)
        {
            def questionnaireInstance = new QuestionnaireInstance()
            questionnaireInstance.type = questionnaire?.type
            questionnaireInstance.opportunity = opportunity
            questionnaireInstance.questionnaire = questionnaire
            questionnaireInstance.description = questionnaire?.description
            questionnaireInstance.name = questionnaire?.name
            if (questionnaireInstance.validate())
            {
                questionnaireInstance.save flush: true
            }
            else
            {
                println questionnaireInstance.errors
            }

            def fixedQuestionList = Question.findAllByQuestionnaireAndType(questionnaire, 'fixed')
            initQuestionInstance(fixedQuestionList, questionnaireInstance)

            def questionList = Question.findAllByQuestionnaireAndType(questionnaire, 'random')
            def randomQuestionList = getRandomQuestionList(questionList)
            initQuestionInstance(randomQuestionList, questionnaireInstance)
        }
    }

    def getRandomQuestionList(List paramList)
    {
        if (paramList?.size() < 3)
        {
            return paramList
        }
        def questionList = []
        while (questionList?.size() != 3)
        {
            int temp = new Random().nextInt(paramList?.size())
            if (!questionList?.contains(paramList[temp]))
            {
                questionList.add(paramList[temp])
            }
        }
        questionList
    }

    def initQuestionInstance(List questionList, QuestionnaireInstance questionnaireInstance)
    {
        questionList?.each {
            def questionInstance = new QuestionInstance()
            questionInstance.name = it?.name
            questionInstance.type = it?.type
            questionInstance.questionType = it?.questionType
            questionInstance.displayOrder = it?.displayOrder
            questionInstance.questionnaireInstance = questionnaireInstance
            if (questionInstance.validate())
            {
                questionInstance.save flush: true
            }
            else
            {
                println questionInstance.errors
            }

            def questionOptionList = Option.findAllByQuestion(it)
            questionOptionList?.each {
                def optionInstance = new OptionInstance()
                optionInstance.name = it?.name
                optionInstance.displayOrder = it?.displayOrder
                optionInstance.questionInstance = questionInstance
                if (optionInstance.validate())
                {
                    optionInstance.save flush: true
                }
                else
                {
                    println optionInstance.errors
                }
            }
        }
    }
}